<?php

namespace App\Entity\Shop;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Entity\MyData\MyDataTemplate;
use App\Entity\Sale\PromotionProduct;
use App\Repository\Shop\ShopProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=ShopProductRepository::class)
 */
class ShopProduct
{
    /**
     * @Common(cnName="#",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"prod","product"})
     */
    private $id;

    /**
     * @Common(cnName="标题",listType="text",editType="text",width="")
     * @Groups({"prod","product"})
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @Common(cnName="副标题",listType="text",editType="text",width="200")
     * @Groups({"prod","product"})
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $subTitle;

    /**
     * @Common(cnName="封面图",listType="pic",editType="pic",width="200")
     * @Groups({"prod","product"})
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $cover;

    /**
     * @Common(cnName="视频地址",listType="text",editType="file",width="200")
     * @Groups({"prod","product"})
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $video;

    /**
     * @Common(cnName="详情图",listType="text",editType="pics",width="200")
     * @Groups({"prod","product"})
     * @ORM\Column(type="array", nullable=true)
     */
    private $pics = [];

    /**
     * @Common(cnName="文本说明",listType="text",editType="textarea",width="200")
     * @Groups({"prod","product"})
     * @ORM\Column(type="text", nullable=true)
     */
    private $intro;

    /**
     * @Common(cnName="浏览量",listType="text",editType="text",width="90")
     * @Groups({"prod","product"})
     * @ORM\Column(type="integer", nullable=true)
     */
    private $views;

    /**
     * @Common(cnName="销量",listType="text",editType="text",width="90")
     * @Groups({"prod","product"})
     * @ORM\Column(type="float", nullable=true)
     */
    private $sell;

    /**
     * @Common(cnName="参数模板",listType="text",editType="select",width="90")
     * @Groups({"prod","product"})
     * @ORM\ManyToOne(targetEntity=MyDataTemplate::class)
     */
    private $paramTemplate;

    /**
     * @Common(cnName="分类目录",listType="text",editType="select",width="150")
     * @Groups({"prod","product"})
     * @ORM\ManyToOne(targetEntity=ShopCategory::class, inversedBy="shopProducts")
     * @ORM\JoinColumn(nullable=true)
     */
    private $category;

    /**
     * @ORM\OneToMany(targetEntity=ShopProductStorage::class, mappedBy="product", orphanRemoval=true)
     */
    private $storages;

    /**
     * @ORM\OneToMany(targetEntity=PromotionProduct::class, mappedBy="product", orphanRemoval=true)
     */
    private $promotions;

    public function __construct()
    {
        $this->storages = new ArrayCollection();
        $this->promotions = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getSubTitle(): ?string
    {
        return $this->subTitle;
    }

    public function setSubTitle(?string $subTitle): self
    {
        $this->subTitle = $subTitle;

        return $this;
    }

    public function getCover(): ?string
    {
        return $this->cover;
    }

    public function setCover(?string $cover): self
    {
        $this->cover = $cover;

        return $this;
    }

    public function getVideo(): ?string
    {
        return $this->video;
    }

    public function setVideo(?string $video): self
    {
        $this->video = $video;

        return $this;
    }

    public function getPics(): ?array
    {
        return $this->pics;
    }

    public function setPics(?array $pics): self
    {
        $this->pics = $pics;

        return $this;
    }

    public function getIntro(): ?string
    {
        return $this->intro;
    }

    public function setIntro(?string $intro): self
    {
        $this->intro = $intro;

        return $this;
    }

    public function getViews(): ?int
    {
        return $this->views;
    }

    public function setViews(?int $views): self
    {
        $this->views = $views;

        return $this;
    }

    public function getSell(): ?float
    {
        return $this->sell;
    }

    public function setSell(?float $sell): self
    {
        $this->sell = $sell;

        return $this;
    }

    public function getParamTemplate(): ?MyDataTemplate
    {
        return $this->paramTemplate;
    }

    public function setParamTemplate(?MyDataTemplate $paramTemplate): self
    {
        $this->paramTemplate = $paramTemplate;

        return $this;
    }

    public function getCategory(): ?ShopCategory
    {
        return $this->category;
    }

    public function setCategory(?ShopCategory $category): self
    {
        $this->category = $category;

        return $this;
    }

    /**
     * @return Collection|ShopProductStorage[]
     */
    public function getStorages(): Collection
    {
        return $this->storages;
    }

    public function addStorage(ShopProductStorage $storage): self
    {
        if (!$this->storages->contains($storage)) {
            $this->storages[] = $storage;
            $storage->setProduct($this);
        }

        return $this;
    }

    public function removeStorage(ShopProductStorage $storage): self
    {
        if ($this->storages->contains($storage)) {
            $this->storages->removeElement($storage);
            // set the owning side to null (unless already changed)
            if ($storage->getProduct() === $this) {
                $storage->setProduct(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|PromotionProduct[]
     */
    public function getPromotions(): Collection
    {
        return $this->promotions;
    }

    public function addPromotion(PromotionProduct $promotion): self
    {
        if (!$this->promotions->contains($promotion)) {
            $this->promotions[] = $promotion;
            $promotion->setProduct($this);
        }

        return $this;
    }

    public function removePromotion(PromotionProduct $promotion): self
    {
        if ($this->promotions->contains($promotion)) {
            $this->promotions->removeElement($promotion);
            // set the owning side to null (unless already changed)
            if ($promotion->getProduct() === $this) {
                $promotion->setProduct(null);
            }
        }

        return $this;
    }
}
